package com.ev.apis.controller.asl;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.validation.Valid;


import cn.hutool.core.util.StrUtil;
import com.ev.asl.service.MoldingBarcodeService;
import com.ev.asl.vo.MoldingBarcodePageParam;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.mes.dao.ProductionPlanDao;
import com.ev.mes.domain.ProductionPlanDO;
import com.ev.system.domain.RoleDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.ev.framework.utils.R;

import java.nio.charset.Charset;
import java.util.HashMap;


@Api(value = "/", tags = "对接打码枪信息API")
@RestController
public class MoldingBarcodeApiController {

    /**queryCtWpDatabase/query
     * 对接打码枪信息 服务类
     */
    @Autowired
    private ProductionPlanDao productionPlanDao;
    @Autowired
    private MoldingBarcodeService moldingBarcodeService;
//
//    @EvApiByToken(value = "/apis/moldingBarcode/gainMsg", method = RequestMethod.POST, apiTitle = "拉取打码枪打码信息")
//    @ApiOperation(value = "拉取打码枪打码信息", produces = MediaType.APPLICATION_JSON_VALUE)
//    public R pageList(@Valid @RequestBody @ApiParam("对接打码枪信息列表参数对象") MoldingBarcodePageParam pageParam) {
//        return moldingBarcodeService.pageList(pageParam);
//    }
//
//    @EvApiByToken(value = "/apis/moldingBarcode/callBack", method = RequestMethod.POST, apiTitle = "接收打码枪返回信息")
//    @ApiOperation("接收打码枪返回信息")
//    public R callBack(@Valid @RequestBody @ApiParam("对接打码枪信息列表参数对象")MoldingBarcodePageParam pageParam) {
//        return moldingBarcodeService.callBackByParam(pageParam);
//    }


    @EvApiByToken(value = "/apis/moldingBarcode/callBack/lotNo", method = RequestMethod.POST, apiTitle = "接收打码枪编码信息")
    @ApiOperation("接收打码枪编码信息")
    public R callBackOfCode(@Valid @RequestBody @ApiParam("对接打码枪信息列表参数对象")String  pageParam) {

        return moldingBarcodeService.callBackOfCode(pageParam);
    }


    @EvApiByToken(value = "/apis/moldingBarcode/callBack/listProduction", method = RequestMethod.GET, apiTitle = "查看能够匹配条码的生产计划单")
    @ApiOperation("查看能够匹配条码的生产计划单")
    public R listProduction(
            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "20") int pagesize) {

        HashMap<String, Object> planMap = new HashMap<>();
        planMap.put("offset", (pageno - 1) * pagesize);
        planMap.put("limit", pagesize);
        planMap.put("pageno", pageno);
        planMap.put("pagesize", pagesize);
        return moldingBarcodeService.listProduction(planMap);
    }

    @EvApiByToken(value = "/apis/moldingBarcode/callBack/updateProduction", method = RequestMethod.GET, apiTitle = "结案生产计划单")
    @ApiOperation("结案生产计划单")
    public R updateProduction(
            @ApiParam(value = "生产计划id", required = true) @RequestParam(value = "id", defaultValue = "1") Long id) {

        ProductionPlanDO plan = new ProductionPlanDO();
        plan.setId(id);
        plan.setBagStatus(1);
        productionPlanDao.update(plan);
        return R.ok("结案成功");
    }


    /**
     * 加解密统一编码方式
     */
    private final static String ENCODING = "utf-8";

    /**
     * 加解密方式
     */
    private final static String ALGORITHM  = "AES";

    /**
     *加密模式及填充方式
     */
    private final static String PATTERN = "AES/CBC/PKCS5Padding";

//    /**
//     * AES加密
//     * @param plainText
//     * @param key
//     * @return
//     * @throws Exception
//     */
//    public static String encrypt(String plainText, String key) throws Exception {
//        if (key == null) {
//            System.out.print("Key为空null");
//            return null;
//        }
//        // 判断Key是否为16位
//        if (key.length() != 16) {
//            System.out.print("Key长度不是16位");
//            return null;
//        }
//        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM);
//        // AES加密采用pkcs5padding填充
//        Cipher cipher = Cipher.getInstance(PATTERN);
//        //用密匙初始化Cipher对象
//        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//        //执行加密操作
//        byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING));
//        return Base64.getEncoder().encodeToString(encryptData);
//    }
//
//
//    /**
//     * AES解密
//     * @param plainText
//     * @param key
//     * @return
//     * @throws Exception
//     */
//    public static String decrypt(String plainText, String key) throws Exception {
//        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM);
//        // 获取 AES 密码器
//        Cipher cipher = Cipher.getInstance(PATTERN);
//        // 初始化密码器（解密模型）
//        cipher.init(Cipher.DECRYPT_MODE, secretKey);
//        // 解密数据, 返回明文
//        byte[] encryptData = cipher.doFinal(Base64.getDecoder().decode(plainText));
//        return new String(encryptData,ENCODING);
//    }

    public static String aesEncrypt(String content, String aesKey, String charset){

        if (StrUtil.isBlank(content)) {
            return content;
        }
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //
            IvParameterSpec iv = new IvParameterSpec("0000000000000000".getBytes()); //
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey.getBytes(), "AES"), iv); //
            byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
            return new String(Base64.encodeBase64(encryptBytes));
        } catch (Exception e) {
            return "失败";
        }
        }

    public static void main(String[] args) {
        String encrypt = aesEncrypt("213", "dolycnodnxciwyvm", "utf-8");
        System.out.println(encrypt);
    }


}
